<?php

class StudentTransfer_TablesController extends Zend_Controller_Action
{

    public function init()
    {
        $locale = Bootstrap::$config['resources']['locale']['default'];
        $path = APPLICATION_PATH."/modules/student/languages/$locale/data.ini";
        $ini = new Zend_Config_Ini($path);
        $this->view->arrayData = $ini->get('array_data')->toArray();
    }

    public function indexAction()
    {
        $temp = explode("-", $this->view->Semester['yearSeme']);
        $year= (int)$temp[0];
        $semester = (int)$temp[1];
        // 統計在籍數
        $res = Doctrine_Query::create()
        ->select('COUNT(*) AS cc ,  a.sex, b.grade AS grade')
        ->from('Student_Model_Student a')
        ->leftJoin('a.Student_Model_StudentSemester b')
        ->where('b.year=?', $year)
        ->andWhere('b.semester=?', $semester)
        ->andWhereIn('a.study_condition',
        array(Student_Model_StudentTransfer::IN_STADY,Student_Model_StudentTransfer::HOMESCHOOLING))
        ->groupby('a.sex, b.grade')
        ->fetchArray();
        $arr = array();
        $arr['count']['M'] = 0;
        $arr['count']['F'] = 0;
        foreach($res as $row) {
            $arr[$row['grade']][$row['sex']] = $row['cc'];
            $arr['count'][$row['sex']] += $row['cc'];
        }
        $this->view->inRefisterData = $arr;

        // 統計異動數
        $res = Doctrine_Query::create()
        ->select('COUNT(*) AS cc, a.causation_kind , b.sex AS sex,c.grade AS grade')
        ->from ('Student_Model_StudentTransfer a')
        ->leftJoin('a.Student_Model_Student b')
        ->leftJoin('b.Student_Model_StudentSemester c')
        ->where('a.year=?', $year)
        ->andwhere('a.semester=?', $semester)
        ->groupBy('a.causation_kind, b.sex, c.grade')
        ->fetchArray();

        $arr = array();
        foreach($res as $row) {
            if (!isset($arr[$row['causation_kind']]['count'][$row['sex']]))
            $arr[$row['causation_kind']]['count'][$row['sex']] = 0;

            $arr[$row['causation_kind']][$row['grade']][$row['sex']] = $row['cc'];
            $arr[$row['causation_kind']]['count'][$row['sex']]  += $row['cc'];
        }

        $this->view->data = $arr;
    }


}

